import TT from './data';
import {
  information
} from '../api/index'
const app = getApp()
Component({
  data: {
    active: 0,
    isRead: false,
    list: TT.TabMenu,
    isLogin:false
  },
  attached() {
    const token = wx.getStorageSync('token')
    if (token) {
      app.getUser().then(() => {
        this.setListData(true)
      }).catch(() => {
        this.setListData(false)
      })
    }
  },
  methods: {
    onChange(event) {
      let index = event.detail.value
      wx.switchTab({
        url: this.data.list[index].url.startsWith('/') ?
          this.data.list[index].url : `/${this.data.list[index].url}`,
      });
    },
    init() {
      const islogin = wx.getStorageSync('userInfo')
      this.setListData(islogin)
      if (islogin) {
        information.gettosystemnoticemessagebypage().then(res => {
          const arr = res.Result.Datas
          const isRead = arr.some(e => !e.IsRead)
          this.setData({
            isRead
          })
          wx.setStorageSync('isRead', isRead)
        })
      }
      const page = getCurrentPages().pop();
      const route = page ? page.route.split('?')[0] : '';
      const active = this.data.list.findIndex(
        (item) =>
        (item.url.startsWith('/') ? item.url.substr(1) : item.url) ===
        `${route}`,
      );
      this.setData({
        active
      });
    },
    setListData(isLogin) {
      this.setData({
        isLogin:isLogin?true:false
      })
    },
    readMseege() {
      this.setData({
        isRead: false
      })
    }
  },

});